<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input v-model="queryParams.openId" placeholder="请输入openId" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否开通vip" prop="isVip">
         <el-select v-model="queryParams.isVip" placeholder="请选择">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
         </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="是否开通vip" align="center" prop="isVip">
        <template v-slot="scope">
          <span>{{ scope.row.isVip}}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="图片" align="center" prop="urls" width="110">
        <template v-slot="scope">
          <div class="imgbox" v-if="scope.row.urls.length !== 0">
            <img class="imgstyle" :src="scope.row.urls[scope.row.imgIndex]" alt="">
            <div class="toleftstyle" @click="toleft(scope.$index,scope.row.urls.length,scope.row.imgIndex)"><i class="el-icon-arrow-left"></i></div>
            <div class="torightstyle" @click="toright(scope.$index,scope.row.urls.length,scope.row.imgIndex)" ><i class="el-icon-arrow-right"></i></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import { createUser, updateUser, deleteUser, getUser, getUserPage, exportUserExcel } from "@/api/qd/user";

export default {
  name: "User",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序-用户列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        createTime: [],
        openId: null,
        nickName: null,
        phone: null,
        isVip: null,
        vipTime: [],
        avatar: null,
        searchs: null,
        vipCode: null,
        deductionPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openId: [{ required: true, message: "openId不能为空", trigger: "blur" }],
      },
      vipvalue:{},
      openvip:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      this.$axios({url:'/qd/discount/page',method:'get',params:this.queryParams}).then(response => {
        this.list = response.data.records ?? []
        this.total = response.data.total;
        this.loading = false;
        this.list.forEach(item=>{
          this.$set(item,'imgIndex',0)
        })
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        openId: undefined,
        nickName: undefined,
        phone: undefined,
        isVip: undefined,
        vipTime: undefined,
        avatar: undefined,
        searchs: undefined,
        vipCode: undefined,
        deductionPrice: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    toleft(index,imglength,iindex){  // 图片向右
        if(iindex === 0) return this.list[index].imgIndex = imglength - 1 ?? 0
        this.list[index].imgIndex -= 1
    },
    toright(index,imglength,iindex){  // 图片向左
        if(imglength - 1 === iindex) return this.list[index].imgIndex = 0
        this.list[index].imgIndex += 1
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序-用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小程序-用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateUser(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createUser(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios({url:'/qd/discount/delete?id='+ id,method:'delete'}).then(res=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          })
          
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有小程序-用户数据项?').then(() => {
          this.exportLoading = true;
          return exportUserExcel(params);
        }).then(response => {
          this.$download.excel(response, '小程序-用户.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped lang="scss" >
@import "../../styles/minImgStyle.scss";
</style>
